import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class Excercises {
  private final static int WORKDAYS = 5;
  private HashSet<Ex> metabolicSet;
  private HashSet<Ex> weightliftingSet;
  private HashSet<Ex> gymnasticSet;

  public Excercises(HashSet<Ex> metabolicSet, HashSet<Ex> weightliftingSet, HashSet<Ex> gymnasticSet) {
    this.metabolicSet = metabolicSet;
    this.weightliftingSet = weightliftingSet;
    this.gymnasticSet = gymnasticSet;
  }

  public List<List> generate(int trainingPeriod) {
    int times = trainingPeriod / WORKDAYS + trainingPeriod % WORKDAYS;
    List<Ex> excercises = new ArrayList<Ex>();
    List<List> excerciseForToday = new ArrayList<List>();
    List<Ex> metabolic = new ArrayList<Ex>(metabolicSet);
    List<Ex> gymnastic = new ArrayList<Ex>(gymnasticSet);
    List<Ex> weightlifting = new ArrayList<Ex>(weightliftingSet);
    int typeOfexc = 0; //Types: 0-metabolic, 1- gymnastic, 2-weightlifting;

    for(int split = 0; split < times; split++){
      if (split % 3 == 0 || metabolic.size() < 3|| gymnastic.size() < 3 || weightlifting.size() < 3) {
        metabolic = new ArrayList<Ex>(metabolicSet);
        gymnastic = new ArrayList<Ex>(gymnasticSet);
        weightlifting = new ArrayList<Ex>(weightliftingSet);
        Collections.shuffle(metabolic);
        Collections.shuffle(gymnastic);
        Collections.shuffle(weightlifting);
      }

      for(int workday = 1; workday < WORKDAYS + 1; workday++){
        if(typeOfexc > 2){
          typeOfexc = 0;
        }
        for(int excForToday = 0; excForToday < workday; excForToday++){
          if(workday == 4){
            excForToday += 2;
          }
          if(workday == 5){
            excForToday += 4;
          }
          switch(typeOfexc){
            case 0:
              typeOfexc++;
              excercises.add(metabolic.remove(0));
              break;
            case 1:
              typeOfexc++;
              excercises.add(gymnastic.remove(0));
              break;
            case 2:
              excercises.add(weightlifting.remove(0));
              typeOfexc=0;
              break;
          }

        }
        excerciseForToday.add(excercises);
        if (split * WORKDAYS + workday == trainingPeriod) break;
        excercises = new ArrayList<Ex>();
      }
      typeOfexc++;
    }
    return excerciseForToday;
  }
}
